/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.ejb.persistence;

import java.io.Serializable;
import java.util.Date;
import java.util.List;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToMany;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;
import javax.persistence.Temporal;

/**
 *
 * @author Julien
 */
@Entity
@Table(name="BOOK")
@NamedQueries({
        @NamedQuery(name="Book.findAll", query="SELECT b FROM Book b WHERE b.available = true"),
        @NamedQuery(name="Book.findByCategory", query="SELECT b FROM Book b WHERE b.category = :category AND b.available = true"),
        @NamedQuery(name="Book.findBooksUnderThreshold", query="SELECT b FROM Book b WHERE b.available = true AND b.stock <= b.threshold"),
        @NamedQuery(name="Book.findByKey", query="SELECT b FROM Book b WHERE b.available = true AND (b.title LIKE :key OR b.summary LIKE :key)"),
        @NamedQuery(name="Book.findByCategKey", query="SELECT b FROM Book b WHERE b.available = true AND b.category = :category AND (b.title LIKE :key OR b.summary LIKE :key) "),
        @NamedQuery(name="Book.getStateStock", query="SELECT b FROM Book b WHERE b.stock <= b.threshold ")})
public class Book implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name="Id_Book")
    private Long id;

    @Column(name="Title")
    private String title;
    
    @Column(name="Price")
    private Float price;
    
    @Column(name="Stock")
    private Integer stock;
    
    @Column(name="Threshold")
    private Integer threshold = 10;
    
    @Column(name="Cover")
    private String cover;
    
    @Column(name="Editor")
    private String editor;

    @Temporal(javax.persistence.TemporalType.DATE)
    @Column(name="Publication_Date")
    private Date publicationDate;
    
    @Column(name="Summary")
    private String summary = "Résumé indisponible";
    
    @Column(name="Table_Contents")
    private String contents = "Sommaire indisponible";
    
    @Column(name="Available")
    private Boolean available = true;
    
    @ManyToMany(mappedBy = "books")
    private List<Author> authors;

    @Column(name="Types")
    @Enumerated(EnumType.STRING)
    private BookCategory category;
    
/* -----------------------------------------------------------------------------
 * ---------------------------GETTERS AND SETTERS-------------------------------
 * ---------------------------------------------------------------------------*/
    
    public Long getId() {
        return id;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public Float getPrice() {
        return price;
    }

    public void setPrice(Float price) {
        this.price = price;
    }

    public Integer getStock() {
        return stock;
    }

    public void setStock(Integer stock) {
        this.stock = stock;
    }

    public Integer getThreshold() {
        return threshold;
    }

    public void setThreshold(Integer threshold) {
        this.threshold = threshold;
    }

    public String getCover() {
        return cover;
    }

    public void setCover(String cover) {
        this.cover = cover;
    }

    public String getEditor() {
        return editor;
    }

    public void setEditor(String editor) {
        this.editor = editor;
    }

    public Date getPublicationDate() {
        return publicationDate;
    }

    public void setPublicationDate(Date publicationDate) {
        this.publicationDate = publicationDate;
    }

    public String getSummary() {
        return summary;
    }

    public void setSummary(String summary) {
        this.summary = summary;
    }

    public String getContents() {
        return contents;
    }

    public void setContents(String contents) {
        this.contents = contents;
    }

    public List<Author> getAuthors() {
        return authors;
    }

    public void setAuthors(List<Author> authors) {
        this.authors = authors;
    }


    public BookCategory getCategory() {
        return category;
    }

    public void setCategory(BookCategory category) {
        this.category = category;
    }

    public Boolean getAvailable() {
        return available;
    }

    public void setAvailable(Boolean available) {
        this.available = available;
    }
  
    @Override
    public int hashCode() {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Book)) {
            return false;
        }
        Book other = (Book) object;
        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "com.ejb.persistence.Book[ id=" + id + " ]";
    }
    
}
